# Clean Environment -------------------------------------------------------
rm(list = ls())

# Load Data ---------------------------------------------------------------

df_full_cjoint <- readRDS("df_full_cjoint.rds") %>% 
  mutate(ResponseId = as.factor(ResponseId),
         attr_judges_L = as.integer(attr_judges_L),
         attr_judges_M = as.integer(attr_judges_M),
         attr_judges_A = as.integer(attr_judges_A),
         attr_media_L = as.integer(attr_media_L),
         attr_media_M = as.integer(attr_media_M),
         attr_media_A = as.integer(attr_media_A)) 

df_cjoint_w_inattentive <- readRDS("df_full_cjoint_w_inattentive.rds") %>% 
  mutate(ResponseId = as.factor(ResponseId),
         attr_judges_L = as.integer(attr_judges_L),
         attr_judges_M = as.integer(attr_judges_M),
         attr_judges_A = as.integer(attr_judges_A),
         attr_media_L = as.integer(attr_media_L),
         attr_media_M = as.integer(attr_media_M),
         attr_media_A = as.integer(attr_media_A)) 

covariates <- read_rds("df_full_uod.rds")

df_imce_full <- readRDS("IMCEs.rds") %>% 
  mutate(ResponseId = as.factor(ResponseId))  %>% 
  left_join(covariates, by = "ResponseId")

# TABLE C.1 ---------------------------------------------------------------

m1 <- lm_robust(
  attr_judges_L ~  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_cjoint_w_inattentive
)

m2 <- lm_robust(
  attr_judges_M ~  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_cjoint_w_inattentive
)

m3 <- lm_robust(
  attr_judges_A ~  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_cjoint_w_inattentive
)

m4 <- lm_robust(
  attr_media_L ~  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_cjoint_w_inattentive
)

m5 <- lm_robust(
  attr_media_M ~  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_cjoint_w_inattentive
)

m6 <- lm_robust(
  attr_media_A ~  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_cjoint_w_inattentive
)


# Output regression results
texreg(list(m1, m2, m3, m4, m5, m6),
       file = "tab_c1.tex",
       custom.header = list("Judges" = 1:3, "Media" = 4:6),
       custom.model.names = c("Liberal","Majoritarian","Authoritarian","Liberal","Majoritarian","Authoritarian"),
       include.ci = FALSE,
       digits = 3,
       booktabs = TRUE,
       include.rmse = FALSE,
       custom.coef.map = list(
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"
         ),
       custom.gof.names = c(NA, NA, "$N$ Choices", "$N$ Respondents"),
       groups = list("Party preference (baseline = PiS)" = 1:4,
                     "Income (baseline = $<$2000 zł)" = 5:9,
                     "Education (baseline = Primary or vocational)" = 10:12,
                     "Financial situation (baseline = Poor/very poor)" = 13:14)
       )

# FIGURE C.1 --------------------------------------------------------------

mm_iterations <- cj(df_full_cjoint, rating0 ~ 
                      attr_gender + attr_age_fac + attr_party + attr_tax + attr_abortion + 
                      attr_judges + attr_media,
                    id = ~ResponseId, estimate = "mm", by = ~choice_factor,
                    feature_labels = list(attr_gender = "Gender",
                                          attr_age_fac = "Age",
                                          attr_party = "Party",
                                          attr_tax = "Taxes",
                                          attr_abortion = "Abortion",
                                          attr_judges = "Judges",
                                          attr_media = "Media"),
                    level_order = 'desc')

mm_plot <- plot(mm_iterations, group = "choice_factor",legend_title = "Choice #")

ggsave(mm_plot, file = "fig_c1.pdf",width = 6, height = 10)

# FIGURE C.2 --------------------------------------------------------------

mm_main <- cj(df_full_cjoint, chosen ~ 
                      attr_gender + attr_age_fac + attr_party + attr_tax + attr_abortion + 
                      attr_judges + attr_media,
                    id = ~ResponseId, estimate = "amce",
                    feature_labels = list(attr_gender = "Gender",
                                          attr_age_fac = "Age",
                                          attr_party = "Party",
                                          attr_tax = "Taxes",
                                          attr_abortion = "Abortion",
                                          attr_judges = "Judges",
                                          attr_media = "Media"),
              level_order = 'desc')

mm_plot <- plot(mm_main)
mm_plot
ggsave(mm_plot, file = "fig_c2.pdf",width = 6, height = 7)

# FIGURE C.3 --------------------------------------------------------------

mm_main <- cj(df_full_cjoint, rating0 ~ 
                attr_gender + attr_age_fac + attr_party + attr_tax + attr_abortion + 
                attr_judges + attr_media,
              id = ~ResponseId, estimate = "amce",
              feature_labels = list(attr_gender = "Gender",
                                    attr_age_fac = "Age",
                                    attr_party = "Party",
                                    attr_tax = "Taxes",
                                    attr_abortion = "Abortion",
                                    attr_judges = "Judges",
                                    attr_media = "Media"),
              level_order = 'desc')

mm_plot <- plot(mm_main)
mm_plot

ggsave(mm_plot, file = "fig_c3.pdf",width = 6, height = 7)

# FIGURE C.4 --------------------------------------------------------------

mm_partisan <- cj(df_full_cjoint, rating0 ~ 
                attr_gender + attr_age_fac + attr_party + attr_tax + attr_abortion + 
                attr_judges + attr_media,
              id = ~ResponseId, estimate = "mm", by = ~party_overlap,
              feature_labels = list(attr_gender = "Gender",
                                    attr_age_fac = "Age",
                                    attr_party = "Party",
                                    attr_tax = "Taxes",
                                    attr_abortion = "Abortion",
                                    attr_judges = "Judges",
                                    attr_media = "Media"),
              level_order = 'desc')

mm_partisan_plot <- plot(mm_partisan,group = "party_overlap",legend_title = "Party overlap")
mm_partisan_plot

ggsave(mm_partisan_plot, file = "fig_c4.pdf",width = 6, height = 7)

# FIGURE C.5 --------------------------------------------------------------

leftcamp <- c("Civic Coalition", "The Left", "Polish Coalition", "Poland 2050")
rightcamp <- c("Law and Justice (PiS)", "Confederation")

df_full_cjoint %>% group_by(ResponseId,choice) %>% 
  mutate(left_camp = case_when(attr_party %in% leftcamp ~ 1),
         right_camp = case_when(attr_party %in% rightcamp ~ 1),
         left_camp_choice = as.factor(case_when(duplicated(left_camp) &  left_camp == party_preference %in% leftcamp ~ "Yes",
                                      TRUE ~ "No")),
         right_camp_choice = as.factor(case_when(duplicated(right_camp) & right_camp == party_preference %in% rightcamp ~ "Yes",
                                       TRUE ~ "No"))
  ) -> df_full_cjoint_camps

# a)

df_full_cjoint_camps %>% filter(left_camp == 1) -> df_left_camp

mm_main <- cj(df_left_camp, rating0 ~ 
                attr_gender + attr_age_fac + attr_party + attr_tax + attr_abortion + 
                attr_judges + attr_media,
              id = ~ResponseId, estimate = "amce", by = ~left_camp_choice,
              feature_labels = list(attr_gender = "Gender",
                                    attr_age_fac = "Age",
                                    attr_party = "Party",
                                    attr_tax = "Taxes",
                                    attr_abortion = "Abortion",
                                    attr_judges = "Judges",
                                    attr_media = "Media"),
              level_order = 'desc')

mm_plot <- plot(mm_main, group = "left_camp_choice",legend_title = "Left-camp choice")
mm_plot

ggsave(mm_plot, file = "fig_c5a.pdf",width = 6, height = 7)

# b) 

df_full_cjoint_camps %>% filter(right_camp == 1) -> df_right_camp

mm_main <- cj(df_right_camp, rating0 ~ 
                attr_gender + attr_age_fac + attr_party + attr_tax + attr_abortion + 
                attr_judges + attr_media,
              id = ~ResponseId, estimate = "amce", by = ~right_camp_choice,
              feature_labels = list(attr_gender = "Gender",
                                    attr_age_fac = "Age",
                                    attr_party = "Party",
                                    attr_tax = "Taxes",
                                    attr_abortion = "Abortion",
                                    attr_judges = "Judges",
                                    attr_media = "Media"),
              level_order = 'desc')

mm_plot <- plot(mm_main, group = "right_camp_choice",legend_title = "Right-camp choice")
mm_plot

ggsave(mm_plot, file = "fig_c5b.pdf",width = 6, height = 7)

# TABLE C.2 ---------------------------------------------------------------

m1 <- lm_robust(judges_M_naive ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m2 <- lm_robust(judges_A_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

m3 <- lm_robust(media_M_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m4 <- lm_robust(media_A_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

texreg(list(m1,m2,m3,m4),
       file = "tab_c2.tex",
       include.ci = F,
       digits = 3,
       custom.header = list("Judges" = 1:2, "Media" = 3:4),
       custom.model.names = c("Majoritarian","Authoritarian","Majoritarian","Authoritarian"),
       custom.coef.map = list(
         "lib" = "Liberal understanding",
         "maj" = "Majoritarian understanding",
         "auth" = "Authoritarian understanding",
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       booktabs = T,
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 4:8,
                     "Income (baseline = $<$2000 zł)" = 9:13,
                     "Education (baseline = Primary or vocational)" = 14:16,
                     "Financial situation (baseline = Poor/very poor)" = 17:18))

m1_lm <- lm(judges_M_naive ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
vif_values <- vif(m1_lm)
print(vif_values)

# TABLE C.3 ---------------------------------------------------------------

m1 <- lm_robust(judges_M_naive ~ lib + maj + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m2 <- lm_robust(judges_A_naive ~ lib  + maj + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

m3 <- lm_robust(media_M_naive ~ lib  + maj + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m4 <- lm_robust(media_A_naive ~ lib  + maj + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

texreg(list(m1,m2,m3,m4),
       file = "tab_c3.tex",
       include.ci = F,
       digits = 3,
       custom.header = list("Judges" = 1:2, "Media" = 3:4),
       custom.model.names = c("Majoritarian","Authoritarian","Majoritarian","Authoritarian"),
       custom.coef.map = list(
         "lib" = "Liberal understanding",
         "maj" = "Majoritarian understanding",
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       booktabs = T,
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 3:7,
                     "Income (baseline = $<$2000 zł)" = 8:12,
                     "Education (baseline = Primary or vocational)" = 13:15,
                     "Financial situation (baseline = Poor/very poor)" = 16:17))

# TABLE C.4 ---------------------------------------------------------------

m1 <- lm_robust(judges_M_naive ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1,])
m2 <- lm_robust(judges_A_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1,])

m3 <- lm_robust(media_M_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1,])
m4 <- lm_robust(media_A_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1,])

texreg(list(m1,m2,m3,m4),
       file = "tab_c4.tex",
       include.ci = F,
       digits = 3,
       custom.header = list("Judges" = 1:2, "Media" = 3:4),
       custom.model.names = c("Majoritarian","Authoritarian","Majoritarian","Authoritarian"),
       custom.coef.map = list(
         "lib" = "Liberal understanding",
         "maj" = "Majoritarian understanding",
         "auth" = "Authoritarian understanding",
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       booktabs = T,
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 4:8,
                     "Income (baseline = $<$2000 zł)" = 9:13,
                     "Education (baseline = Primary or vocational)" = 14:16,
                     "Financial situation (baseline = Poor/very poor)" = 17:18))

# TABLE C.5 ---------------------------------------------------------------

m1 <- lm_robust(judges_M_naive ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1 & df_imce_full$check_2_pass==1,])
m2 <- lm_robust(judges_A_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1 & df_imce_full$check_2_pass==1,])

m3 <- lm_robust(media_M_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1 & df_imce_full$check_2_pass==1,])
m4 <- lm_robust(media_A_naive ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full[df_imce_full$check_1_pass==1 & df_imce_full$check_2_pass==1,])

texreg(list(m1,m2,m3,m4),
       file = "tab_c5.tex",
       include.ci = F,
       digits = 3,
       custom.header = list("Judges" = 1:2, "Media" = 3:4),
       custom.model.names = c("Majoritarian","Authoritarian","Majoritarian","Authoritarian"),
       custom.coef.map = list(
         "lib" = "Liberal understanding",
         "maj" = "Majoritarian understanding",
         "auth" = "Authoritarian understanding",
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       booktabs = T,
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 4:8,
                     "Income (baseline = $<$2000 zł)" = 9:13,
                     "Education (baseline = Primary or vocational)" = 14:16,
                     "Financial situation (baseline = Poor/very poor)" = 17:18))

# TABLE C.6 ---------------------------------------------------------------

m1 <- lm_robust(lib ~ party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m2 <- lm_robust(maj ~ party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m3 <- lm_robust(auth ~ party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

texreg(list(m1,m2,m3),
       file = "tab_c6.tex",
       include.ci = F,
       digits = 3,
       custom.model.names = c("Liberal","Majoritarian","Authoritarian"),
       custom.coef.map = list(
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       booktabs = T,
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 1:5,
                     "Income (baseline = $<$2000 zł)" = 6:10,
                     "Education (baseline = Primary or vocational)" = 11:13,
                     "Financial situation (baseline = Poor/very poor)" = 14:15))

# C.7 ---------------------------------------------------------------------

m1 <- lm_robust(
  bench_judges_lib ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_imce_full
)

m2 <- lm_robust(
  bench_judges_maj ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_imce_full
)

m3 <- lm_robust(
  bench_judges_auth ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_imce_full
)

m4 <- lm_robust(
  bench_media_lib ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_imce_full
)

m5 <- lm_robust(
  bench_media_maj ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_imce_full
)

m6 <- lm_robust(
  bench_media_auth ~ lib + maj + auth +  party_preference + income + education + financial_situation + gender + age,
  clusters = ResponseId, data = df_imce_full
)


# Output regression results
texreg(list(m1, m2, m3, m4, m5, m6),
       file = "tab_c7.tex",
       custom.header = list("Judges" = 1:3, "Media" = 4:6),
       custom.model.names = c("Liberal","Majoritarian","Authoritarian","Liberal","Majoritarian","Authoritarian"),
       include.ci = FALSE,
       digits = 3,
       booktabs = TRUE,
       custom.coef.map = list(
         "lib" = "Liberal understanding",
         "maj" = "Majoritarian understanding",
         "auth" = "Authoritarian understanding",
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 4:8,
                     "Income (baseline = $<$2000 zł)" = 9:13,
                     "Education (baseline = Primary or vocational)" = 14:16,
                     "Financial situation (baseline = Poor/very poor)" = 17:18))